<?php
namespace app\common\model;




use think\Model;

class UserFollow extends Model
{


    protected function getUserIdAttr($value, $data)
    {
        return hashid($data['user_id'] ?? 0);
    }

    public function getRows($map = [])
    {

        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.follow_user','LEFT')
            ->join('users u','u.user_id = a.follow_user','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid')
            ->paginate();
//            ->each(function ($item){
////                if ($item['online_status']){
////                    $item['status'] = '当前在线';
////                }else{
////                    $item['status'] = formatTime($item['online_time']);
////                }
////                $item['user_id'] = hashid($item['user_id']);
//            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }

    public function getFollow($map = [])
    {

        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.follow_user','LEFT')
            ->join('users u','u.user_id = a.follow_user','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid,e.room_id,e.online_status,e.online_time')
            ->paginate()->each(function ($item){
                if ($item['online_status']){
                    $item['status'] = '当前在线';
                }else{
                    $item['status'] = formatTime($item['online_time']);
                }
//                $item['user_id'] = hashid($item['user_id']);
            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }

    public static function search($map = [])
    {

        return self::alias('a')
//            ->join('user_extend e','e.user_id = a.follow_user','LEFT')
            ->join('users u','u.user_id = a.follow_user','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid,u.sign')
            ->select();

    }


    public function getGroup($map = [])
    {

        $rows =  $this->alias('a')
//            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid')
            ->cache(5)
            ->select();

//        foreach ($rows as $k => $v){
////            if ($v['online_status']){
////                $rows[$k]['status'] = '当前在线';
////            }else{
////                $rows[$k]['status'] = formatTime($v['online_time']);
////            }
////            $rows[$k]['user_id'] = hashid($v['user_id']);
//        }
        return groupByInitials( collection($rows)->toArray(),'nick_name');

    }

    /**
     * @param array $map
     * @param string $user
     * @return array
     * @throws \think\exception\DbException
     */
    public function popularityList($map = [], $user = 'follow_user')
    {
        $field = 'count(user_id) as total,a.user_id,a.follow_user';

        $rows  =  self::alias('a')
            ->where($map)
            ->field($field)
            ->group($user)
            ->cache(15)
            ->order('total desc')
            ->paginate(30)->each(function ($item)use ($user){


                $item['nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item->getData($user));
                $item['header_img'] = \app\api\controller\Base::staticInfo('header_img',$item->getData($user));
                $item['sign'] = \app\api\controller\Base::staticInfo('sign',$item->getData($user));
                $item['headWear'] = \app\api\controller\Base::getHeadWear($item->getData('user_id'));
//                $item['user_id'] = hashid($item[$user]);
                if ($user !== 'user_id'){
                    $item->data('user_id',$item['follow_user'])  ;
                }

                unset($item['follow_user']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

}